package com.moyu.user.service.role.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.moyu.common.core.enums.ResultCode;
import com.moyu.common.security.exception.ServiceException;
import com.moyu.user.domain.role.Role;
import com.moyu.user.domain.role.dto.RoleQueryDTO;
import com.moyu.user.domain.role.dto.RoleSaveDTO;
import com.moyu.user.domain.role.vo.RoleVO;
import com.moyu.user.mapper.role.RoleMapper;
import com.moyu.user.service.role.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色服务层
 *
 * @Author 赵彦博
 * @Date 2024/12/27 15:30
 */
@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int add(RoleSaveDTO roleSaveDTO) {
        List<Role> roleList = new ArrayList<>();
        Role role = new Role();
        try {
            roleList = roleMapper.selectList(new LambdaQueryWrapper<Role>()
                    .eq(Role::getName, roleSaveDTO.getName()));
        } catch (Exception e) {
            log.error("执行sql-查询出错:" + e.getCause());
        }
        if (CollectionUtil.isNotEmpty(roleList)) {
            //用户已经存在
            //自定义的异常   公共的异常类
            throw new ServiceException(ResultCode.AILED_ROLE_EXISTS);
        }
        role.setName(roleSaveDTO.getName());
        try {
            return roleMapper.insert(role);
        } catch (Exception e) {
            log.error("执行sql-新增角色出错:" + e.getCause());
        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        Role role = new Role();
        try {
            role = roleMapper.selectById(id);
        } catch (Exception e) {
            log.error("执行sql-查询出错:" + e);
        }
        if (role == null) {
            throw new ServiceException(ResultCode.FAILED_ROLE_NOT_EXISTS);
        }
        try {
            List<Long> existId = roleMapper.selectUserAndRoleById(id);
            if (!CollectionUtil.isNotEmpty(existId)) {
                return roleMapper.deleteById(id);
            } else {
                return roleMapper.deleteAllById(id);
            }
        } catch (Exception e) {
            log.error("执行sql-删除出错:" + e);
        }
        return 0;
    }

    @Override
    public List<RoleVO> list(RoleQueryDTO roleQueryDTO) {
        PageHelper.startPage(roleQueryDTO.getPageNum(), roleQueryDTO.getPageSize());
        try {
            return roleMapper.selectRoleList(roleQueryDTO);
        } catch (Exception e) {
            log.info("执行sql-查询出错:" + e.getCause());
        } finally {
            PageHelper.clearPage();
        }
        return new ArrayList<>();
    }

    @Override
    public List<RoleVO> listByUserId(RoleQueryDTO roleQueryDTO) {
        PageHelper.startPage(roleQueryDTO.getPageNum(), roleQueryDTO.getPageSize());
        try {
            return roleMapper.selectRoleListByUserId(roleQueryDTO);
        } catch (Exception e) {
            log.info("执行sql-查询出错:" + e.getCause());
        } finally {
            PageHelper.clearPage();
        }
        return new ArrayList<>();
    }

    @Override
    public List<RoleVO> listByUserIdAll(Long id) {
        try {
            return roleMapper.selectRoleListByUserIdAll(id);
        } catch (Exception e) {
            log.info("执行sql-查询出错:" + e.getCause());
        }
        return new ArrayList<>();
    }

    @Override
    public int addRoleWithUser(Long id, List<RoleVO> roles) {
        if (!roles.isEmpty()) {
            return roleMapper.addRoleWithUser(id, roles);
        }
        return 0;
    }
}
